import OpenAI from "openai";
import fs from "fs";

// 在运行环境中读取 github 的 token
const token = "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" // process.env['GITHUB_TOKEN'];
const endpoint = "https://models.inference.ai.azure.com";
const modelName = "gpt-4o"

function getImageDataUrl(imgUrl, imgFormat) {
  const imageBuffer = fs.readFileSync(imgUrl);
  const imageBase64 = imageBuffer.toString('base64')
  return `data:image/${imgFormat};base64,${imageBase64}`;
}

async function main() {
  const client = new OpenAI({ baseURL: endpoint, apiKey: token });

  const response = await client.chat.completions.create({
    model: modelName,
    temperature: 1.0,
    max_tokens: 1000,
    top_p: 1.0,
    messages: [
      {role: 'system', content: '你是一个详细描述图片的好帮手。'},
      {role: 'user', content: [
        {type: 'text', text: '这张图片中有什么？'},
        {type: 'image_url', image_url: {
          url: getImageDataUrl("./test.jpg", "jpg"),
          detail: 'low'
        }}
      ]},
    ]
  })

  console.log(response.choices[0].message.content);
}

main()